<?php

namespace App\Observers;

use App\Models\CustomerInfo;
use App\Jobs\PushClueToLMS;
use App\Jobs\GivePointToRefereeForClue;
use App\Jobs\GivePointToRefereeForDrive;
use App\Jobs\GivePointToRefereeForBuyCar;

class CustomerInfoObserver
{
    public function created(CustomerInfo $customerInfo)
    {
        //异步推送给LMS
        dispatch(new PushClueToLMS($customerInfo));
        if ($customerInfo->referee_id) {
            dispatch(new GivePointToRefereeForClue($customerInfo));
        }
    }

    public function updated(CustomerInfo $customerInfo)
    {
        if (is_null($customerInfo->getOriginal('driver_at')) && $customerInfo->isDirty('driver_at')) {
            dispatch(new GivePointToRefereeForDrive($customerInfo));
        }
        if (is_null($customerInfo->getOriginal('buy_car_at')) && $customerInfo->isDirty('buy_car_at')) {
            dispatch(new GivePointToRefereeForBuyCar($customerInfo));
        }
        if ($customerInfo->isDirty('dealer_id')) {
            dispatch(new PushClueToLMS($customerInfo));
        }
    }
}
